﻿using Bintje.bintje;
using System.Collections.Generic;
using System;
namespace Bintje
{
    public class Layer : RepoObject
    {
        #region Fields

        private string m_layerName;
        private long m_created1;
        private long m_modified1;

        #endregion
        
        #region Properties

        public string LayerName
        {
            get { return m_layerName; }
            set { m_layerName = value; }
        }

        public long Created1
        {
            get { return m_created1; }
            set { m_created1 = value; }
        }
        
        public long Modified1
        {
            get { return m_modified1; }
            set { m_modified1 = value; }
        }

        #endregion

        #region ctor

        public Layer(layer layer)
        {
            m_id = layer.id;
            m_layerName = layer.layerName;
        }

        public Layer(string layerName) 
        {
            m_layerName = layerName;
        }

        public Layer(int id, string layerName)
        {
            m_layerName = layerName;
            m_id = id;
        }

        #endregion

        #region Methods

        public static IList<Layer> GetLayers(imageoSession session)
        {
            IList<Layer> result = new List<Layer>();
            serviceRequest request = new layerRequest();
            request.session = session;
            layerSetResponse response = API.instance.getLayers(request);

            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                for (int i = 0; i < response.layers.Length; i++)
                {
                    Layer layer = new Layer(response.layers[i]);
                    result.Add(layer);
                }
            }
            
            return result;
        }

        public static IList<Layer> GetAllLayers(imageoSession session)
        {
            IList<Layer> result = new List<Layer>();
            serviceRequest request = new layerRequest();
            layerSetResponse response = API.instance.getLayers(request);
            
            //layer[] response = API.instance.getAllLayers(string.Empty);
            if (null != response && response.layers.Length > 0)
            {
                for (int i = 0; i < response.layers.Length; i++)
                {
                    Layer layer = new Layer(response.layers[i]);
                    result.Add(layer);
                }
            }
            return result;
        }

        public bool Create(imageoSession session)
        {
            if (string.IsNullOrEmpty(this.LayerName))
                return false;
            layerRequest request = GetRequest();
            request.session = session;
            layerResponse response = API.instance.makeLayer(request);
            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                this.Id = response.layer.id;
                return true;
            }
            return false;
        }

        private layerRequest GetRequest()
        {
            layerRequest request = new layerRequest();
            request.name = this.LayerName;
            return request;
        }

        internal layer ConvertToAPI()
        {
            layer layer = new layer();
            layer.id = this.Id;
            layer.layerName = this.LayerName;
            return layer;
        }

        internal static layer[] ConvertToAPI(IList<Layer> layersList)
        {
            layer[] layers = (layer[])Array.CreateInstance(typeof(layer), layersList.Count);
            for (int i = 0; i < layersList.Count; i++)
            {
                layers[i] = layersList[i].ConvertToAPI();
            }
            return layers;
        } 

        #endregion
    }
}
